package cn.jbit.order.controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import cn.jbit.order.service.OrderService;
import cn.jbit.pojo.Order;
import cn.jbit.user.service.UserService;

/**
 * 订单controller层
 * @author _Cpxy
 *
 */
@Controller
public class OrderController {
	
	@Autowired
	private OrderService ors;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 首页查询未完成订单
	 * @param num
	 * @param m
	 * @return
	 */
	@RequestMapping("/OrderAll")
	public String showOrder(Integer num,Model m){
		int count = ors.sumOrder();
		int pageNum=0;
		//判断总页数    
		if(count%4==0) {
			pageNum = count/4;
		}else {
			pageNum = count/4+1;
		}
		// 判断是否第一次查询
		if(num==null || num<1) {
			num = 1;
		}else if(num>pageNum) {
			num = pageNum;
		}
		Page<Order> pages = ors.showAll(num);
		m.addAttribute("pages", pages);
		m.addAttribute("count", count);
		m.addAttribute("pageNum", pageNum);
		m.addAttribute("num", num);
		m.addAttribute("o", 2);
		return "order";
	}
	
	//查询当前用户所有订单信息
	@RequestMapping("ord_sel_allInfo")
	@ResponseBody
	public List<Order> ordSelAllInfo(String name) {
		System.out.println("查询当前用户所有订单信息name======>"+name);
		//查询当前userid
		Integer userid = userService.findUserNameToId(name);
		System.out.println("userid==========>"+userid);
		List<Order> orders = ors.showAlls(userid);
		System.out.println("orders.size=============>"+orders.size());
		return orders;
	}
	
	/**  
	  * 按交易状态查询订单
	 * @param tex
	 * @return 
	 */
	@RequestMapping("/FindState")	
	public String findByState(Integer op,Model s,Integer num,Integer o){
		System.out.println("状态码======="+op);
		//订单总记录数
		int count = 0;
		//总页面
		int pageNum=0;
		//订单集合
		Page<Order> pages = null;
		//查询交易完成的订单
		//查询所有订单
			if(op==0){
			count = ors.sumOrderse();
			//判断总页数
			if(count%4==0) {
				pageNum = count/4;
			}else {
				pageNum = count/4+1;
			}
			// 判断是否第一次查询
			if(num==null || num<1) {
				num = 1;
			}else if(num>pageNum) {
				num = pageNum;
			}
			pages = (Page<Order>) ors.showAlls(num);
			s.addAttribute("pages", pages);
			s.addAttribute("count", count);
			s.addAttribute("pageNum", pageNum);
			s.addAttribute("num", num);
			s.addAttribute("o", op);
			return "order";
		}		
		if(op==1) {
			count = ors.sumOrders();			
		//查询交易未完成的订单
		}else if(op==2){
			count = ors.sumOrder();  
		}
			//判断总页数
			if(count%4==0) {
				pageNum = count/4;
			}else {
				pageNum = count/4+1;
			}
			// 判断是否第一次查询
			if(num==null || num<1) {
				num = 1;
			}else if(num>pageNum) {
				num = pageNum;
			}
		    pages = (Page<Order>) ors.showOrderByState(op,num);
			s.addAttribute("pages", pages);
			s.addAttribute("count", count);
			s.addAttribute("pageNum", pageNum);
			s.addAttribute("num", num);
			s.addAttribute("o", op);
			return "order";
	}
	
	@RequestMapping("/findName")
	public String findByName(String tex,Model s,Integer num) {
		System.out.println("======="+tex);
		//总页面
		int pageNum=0;
		//订单集合
		Page<Order> pages = null;
		//根据名称模糊查询订单
		//订单总记录数
		int	count = ors.sumOredersee(tex);
			System.out.println("总记录数为："+count);
			//判断总页数
			if(count%4==0) {
				pageNum = count/4;
			}else {
				pageNum = count/4+1;
			}
			// 判断是否第一次查询
			if(num==null || num<1) {
				num = 1;
			}else if(num>pageNum) {
				num = pageNum;
			}
			pages = (Page<Order>) ors.showOrderByIS(tex, num);
			System.out.println("-----------------"+pages.size());
			s.addAttribute("pages", pages);
			s.addAttribute("count", count);
			s.addAttribute("pageNum", pageNum);
			s.addAttribute("num", num);
			s.addAttribute("o",3);	
			s.addAttribute("tex", tex);
			return "order";
	}
	
	@RequestMapping("/del")	
	public String delt(int id,Model s,Integer num) {
		System.out.println("接收到的删除编号为："+id);
		try {
			ors.deleteOrder(id);
			System.out.println("删除成功！");	
			int count = 0;
			int pageNum=0;
			Page<Order> pages = null;
				count = ors.sumOrderse();
				//判断总页数
				if(count%4==0) {
					pageNum = count/4;
				}else {
					pageNum = count/4+1;
				}
				// 判断是否第一次查询
				if(num==null || num<1) {
					num = 1;
				}else if(num>pageNum) {
					num = pageNum;
				}
				pages = (Page<Order>) ors.showAlls(num);
				s.addAttribute("pages", pages);
				s.addAttribute("count", count);
				s.addAttribute("pageNum", pageNum);
				s.addAttribute("num", num);
				s.addAttribute("o", 0);
				return "order";
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("出现异常！");
			return "404";
		}
	}
}
